package com.hellloop.pim.alphabeat.network;

import java.util.StringTokenizer;

public class PIMMessage {	
	protected int _msgType;
	protected int _oprType;
	protected String _message;
	
	public PIMMessage() {
		_msgType = 0;
		_oprType = 0;
		_message = "";
	}
	
	public PIMMessage(String message) {
		StringTokenizer st = new StringTokenizer(message, ":");
		try {
			_msgType = Integer.parseInt( st.nextToken() );
			_oprType = Integer.parseInt( st.nextToken() );
		}
		catch(Exception e) {
			System.err.println("Error PIMMessage Format");
			_message = message;
		}
		_message = message.substring( message.indexOf(":", 2)+1 );
	}

	public PIMMessage(int msgType, int oprType, String message) {
		_msgType = msgType;
		_oprType = oprType;
		_message = message;
	}
	
	public static PIMMessage decodeMessage(String message) {
		int msgType = 0;
		int oprType = 0;
		StringTokenizer st = new StringTokenizer(message, ":");
		try {
			msgType = Integer.parseInt( st.nextToken() );
			oprType = Integer.parseInt( st.nextToken() );
		}
		catch(Exception e) {
			System.err.println("Error PIMMessage Format");
		}
		String msg = message.substring( message.indexOf(":", 2)+1 );
		
		if( msgType ==MsgType.ConnectionMsg ) {
			return new PIMConnectionMsg(oprType, msg);
		}
		else if( msgType ==MsgType.RoomMsg ) {
			return new PIMRoomMsg(oprType, msg);
		}
		/*else if( msgType ==MsgType.GameMsg ) {
			return new GameMsg(oprType, msg);
		}
		else if( msgType ==MsgType.ActionMsg ) {
			return new ActionMsg(oprType, msg);
		}*/
		else return null;
	}
	
	public void setMessage(String message){
		StringBuffer buf = new StringBuffer(_message);
		if(!_message.equals("")) buf.append(":");
		buf.append(message);
		_message = buf.toString();
	}
	public String getMessage(){
		return _message;
	}
	public int getMsgType() {
		return _msgType; 
	}
	public void setMsgType(int msgType) {
		_msgType = msgType;
	}
	public int getOprType() {
		return _oprType;
	}
	public void setOprType(int oprType) {
		_oprType = oprType;
	}
	public String toString() {
		StringBuffer buff = new StringBuffer();
		buff.append( getMsgType() ).append(":");
		buff.append( getOprType() ).append(":");
		buff.append( _message );
		return buff.toString();
	}
	
	public static class MsgType
	{	
		public static int ConnectionMsg = 1;
		public static int RoomMsg = 2;
		public static int GameMsg = 3;
		public static int ActionMsg = 4;
	}

	//Tempt
	public static class OprType
	{

		//ActionMsg
		public static int GoonAction1 = 401;
		public static int GoonAction2 = 402;
		public static int GoonAction3 = 403;
		public static int YuuAction1 = 404;
		public static int YuuAction2 = 405;
		public static int YuuAction3 = 406;
		public static int AtaAction1 = 407;
		public static int AtaAction2 = 408;
		public static int AtaAction3 = 409;
	}
}
